#include <stdlib.h>

#include "lib.h"

#include "slotguts.h"
#include "monsterguts.h"
#include "slotspin.h"
#include "getrand.h"
#include "sltsql.h"
#include "slotconn.h"
#include "opts.h"
#include "log.h"


static int gMonster=0;

enum WheelContents
{
    BLANK,
    FEET,
    HAND,
    SKULL,
    LIVER,
    INTESTINES,
    EYEBALLS,
    LUNGS,
    HEART,
    BRAIN,
    GRAVE,
};


enum SpinResult
{
	EVT_LOSER = 0,

	EVT_GRAVEBONUS,
	EVT_MBUILDERBONUS,
	EVT_BONUSBOTH,

	EVT_2FEET,
	EVT_2HAND,
	EVT_2SKULL,
	EVT_2LIVER,
	EVT_2INTESTINES,
	EVT_2EYEBALLS,
	EVT_2LUNGS,
	EVT_2HEART,
	EVT_2BRAIN,

	EVT_3FEET,
	EVT_3HAND,
	EVT_3SKULL,
	EVT_3LIVER,
	EVT_3INTESTINES,
	EVT_3EYEBALLS,
	EVT_3LUNGS,
	EVT_3HEART,
	EVT_3BRAIN,

	EVT_4FEET,
	EVT_4HAND,
	EVT_4SKULL,
	EVT_4LIVER,
	EVT_4INTESTINES,
	EVT_4EYEBALLS,
	EVT_4LUNGS,
	EVT_4HEART,
	EVT_4BRAIN,

	EVT_5FEET,
	EVT_5HAND,
	EVT_5SKULL,
	EVT_5LIVER,
	EVT_5INTESTINES,
	EVT_5EYEBALLS,
	EVT_5LUNGS,
	EVT_5HEART,
	EVT_5BRAIN,

	NUMEVENTS
};

SlotSpin *
MonsterSlotGuts::makeJackpot()
{
    uint8 *stops = new uint8[getReelCount()];
    SlotSpin *spin = new SlotSpin();

    spin->setBet(0);
    spin->setBonusGameType(0);


    /**
     * Hard set the reels to jackpots.  The assumption is
     * that the jackpot icon is the second to last icon on
     * the strip (the last being a blank).
     */
    for (uint8 i = 0; i < getReelCount(); i++)
    {
        stops[i] = getIcon(i, getIconCount(i) - 2)->position;
    }

    spin->setStops(getReelCount(), stops);

    classifyResult(spin);

    m_spins.push_back(spin);

    delete [] stops;

    return spin;
}

bool8
MonsterSlotGuts::isValidGuts()
{
    return (getGutsId() >= GUTSIDMIN && getGutsId() < GUTSIDMAX);
}

int32
MonsterSlotGuts::getMaxAggregate()
{
    return MAXAGGREGATE;
}


uint8
MonsterSlotGuts::classifyStops(uint8* stops,uint8 bet, uint8 line)
{
	uint8 result = EVT_LOSER;


	uint8 whichIcon = BLANK;
	uint8 testIcon = BLANK;
	bool8 streak = true; //Every line has a streak of at least one

	//Check the first icon (the rest have to match it)
	whichIcon = getIcon(0, stops[0])->iconid;
	testIcon = whichIcon;

	//Now, go through the rest, until the streak is over
	for ( uint8 i = 1; (i < getReelCount())&&(streak); i++ )
    {
		testIcon = getIcon(i, stops[i])->iconid;
		if(testIcon != whichIcon)
		{
			streak = false;
		}
	}

	//i is now the number of icons in a row (+1 if streak is false),
	//so pay it out
	if(!streak)
	{
		i-=1;
	}
	switch(i)
	{
		case 2:
			switch(whichIcon)
			{
				case FEET:
					result = EVT_2FEET;
					break;
				case HAND:
					result = EVT_2HAND;
					break;
				case SKULL:
					result = EVT_2SKULL;
					break;
				case LIVER:
					result = EVT_2LIVER;
					break;
				case INTESTINES:
					result = EVT_2INTESTINES;
					break;
				case EYEBALLS:
					result = EVT_2EYEBALLS;
					break;
				case LUNGS:
					result = EVT_2LUNGS;
					break;
				case HEART:
					result = EVT_2HEART;
					break;
				case BRAIN:
					result = EVT_2BRAIN;
					break;
			}
			break;
		case 3:
			switch(whichIcon)
			{
				case FEET:
					result = EVT_3FEET;
					break;
				case HAND:
					result = EVT_3HAND;
					break;
				case SKULL:
					result = EVT_3SKULL;
					break;
				case LIVER:
					result = EVT_3LIVER;
					break;
				case INTESTINES:
					result = EVT_3INTESTINES;
					break;
				case EYEBALLS:
					result = EVT_3EYEBALLS;
					break;
				case LUNGS:
					result = EVT_3LUNGS;
					break;
				case HEART:
					result = EVT_3HEART;
					break;
				case BRAIN:
					result = EVT_3BRAIN;
					break;
			}
			break;
		case 4:
			switch(whichIcon)
			{
				case FEET:
					result = EVT_4FEET;
					break;
				case HAND:
					result = EVT_4HAND;
					break;
				case SKULL:
					result = EVT_4SKULL;
					break;
				case LIVER:
					result = EVT_4LIVER;
					break;
				case INTESTINES:
					result = EVT_4INTESTINES;
					break;
				case EYEBALLS:
					result = EVT_4EYEBALLS;
					break;
				case LUNGS:
					result = EVT_4LUNGS;
					break;
				case HEART:
					result = EVT_4HEART;
					break;
				case BRAIN:
					result = EVT_4BRAIN;
					break;
			}
			break;
		case 5:
			switch(whichIcon)
			{
				case FEET:
					result = EVT_5FEET;
					break;
				case HAND:
					result = EVT_5HAND;
					break;
				case SKULL:
					result = EVT_5SKULL;
					break;
				case LIVER:
					result = EVT_5LIVER;
					break;
				case INTESTINES:
					result = EVT_5INTESTINES;
					break;
				case EYEBALLS:
					result = EVT_5EYEBALLS;
					break;
				case LUNGS:
					result = EVT_5LUNGS;
					break;
				case HEART:
					result = EVT_5HEART;
					break;
				case BRAIN:
					result = EVT_5BRAIN;
					break;
			}
			break;
	}

	return result;
}

void MonsterSlotGuts::preSpinClassifyResults(SlotSpin *spin)
{
	SlotGuts::preSpinClassifyResults(spin);

	//Manually traverse through the 15 positions, looking for body parts or grave icons.
	uint8 numGraveIcons = 0;
	uint8 graveReel[3];
	uint8 graveRow[3];
	uint8 bodypartcount[9];
	uint8 stop = 0;
	uint8 whichItem = BLANK;
	int32 count;
	int32 zeroCount;


	memset (graveReel, 0, sizeof(graveReel));
	memset (graveRow, 0, sizeof(graveRow));
	memset (bodypartcount, 0, sizeof(bodypartcount));

	// Go through each reel and each row in the spin
	for(int reel = 0; reel<5; reel++)
	{
		for(int row = 0; row < 3; row++)
		{
			stop = relativeWheelPosition(reel, spin->getStops()[reel], row);
			whichItem = getIcon(reel, stop)->iconid;

			// Increment the bodypart count for this icon
			if (whichItem >= BLANK && whichItem < GRAVE)
			{
				// FEET = index 0, BRAIN= index 8
				++bodypartcount[whichItem-1];
			}
			// For graves, save the reel and row which will be sent to the client.
			else if (whichItem == GRAVE)
			{
				++numGraveIcons;
			}
		} //row
	} //reel

	zeroCount = 0;
	// Set the new bodypart counts, cannot go below 0.  Track the number of body parts
	// that have reached zero.
	for (int i=0; i<9; ++i)
	{
		count = m_subGame->getData (SGIDX_FEET + i);
		count -= bodypartcount[i];
		if (count < 0)
			count = 0;
//		m_subGame->update(SGIDX_FEET + i,count);
		if (count == 0)
			++zeroCount;
	}

	if(numGraveIcons == 3 && zeroCount == 9)
	{
		count = spin->getStopCount();
		uint8 stops[5];
		memcpy (stops, spin->getStops(), count);
		stops[4] = 9;
		spin->setStops(count, stops);
	}
}

void
MonsterSlotGuts::classifyScatters(SlotSpin *spin)
{

	//Manually traverse through the 15 positions, looking for body parts or grave icons.
	uint8 numGraveIcons = 0;
	uint8 graveReel[3];
	uint8 graveRow[3];
	uint8 bodypartcount[9];
	uint8 stop = 0;
	uint8 whichItem = BLANK;
	int32 count;
	int32 zeroCount;


	memset (graveReel, 0, sizeof(graveReel));
	memset (graveRow, 0, sizeof(graveRow));
	memset (bodypartcount, 0, sizeof(bodypartcount));

	// Go through each reel and each row in the spin
	for(int reel = 0; reel<5; reel++)
	{
		for(int row = 0; row < 3; row++)
		{
			stop = relativeWheelPosition(reel, spin->getStops()[reel], row);
			whichItem = getIcon(reel, stop)->iconid;

			// Increment the bodypart count for this icon
			if (whichItem >= BLANK && whichItem < GRAVE)
			{
				// FEET = index 0, BRAIN= index 8
				++bodypartcount[whichItem-1];
			}
			// For graves, save the reel and row which will be sent to the client.
			else if (whichItem == GRAVE)
			{
				graveReel[numGraveIcons] = reel;
				graveRow[numGraveIcons++] = row;
			}
		} //row
	} //reel

	zeroCount = 0;
	// Set the new bodypart counts, cannot go below 0.  Track the number of body parts
	// that have reached zero.
	for (int i=0; i<9; ++i)
	{
		count = m_subGame->getData (SGIDX_FEET + i);
		count -= bodypartcount[i];
		if (count < 0)
			count = 0;
		m_subGame->update(SGIDX_FEET + i,count);
		if (count == 0)
		++zeroCount;
	}

	m_subGame->update(SGIDX_BONUSGAME,BGNONE);
	readytofinish = true;

	if(numGraveIcons == 3)
	{
		// If it's Grave Bonus, initialize the data.  We start with last 5 elements zeroed out.
		// They get set when the player chooses a grave.
		m_subGame->update(SGIDX_BONUSGAME,BGGRAVEBONUS);

		m_subGame->update(SGIDX_REEL1,graveReel[0]);
		m_subGame->update(SGIDX_LINE1,graveRow[0]);

		m_subGame->update(SGIDX_REEL2,graveReel[1]);
		m_subGame->update(SGIDX_LINE2,graveRow[1]);

		m_subGame->update(SGIDX_REEL3,graveReel[2]);
		m_subGame->update(SGIDX_LINE3,graveRow[2]);

		initializeGraveBonus ();
		readytofinish = false;

	}

	// Do we enter the Monster Builder Bonus game
	if (zeroCount == 9)
	{
		// Remember that we can have the case where both bonus games are active
		// during a spin.  In that case we will do the grave bonus and then the
		// Monster builder.
		if (m_subGame->getData (SGIDX_BONUSGAME) != BGGRAVEBONUS)
			m_subGame->update(SGIDX_BONUSGAME,BGBUILDERBONUS);
		else
			m_subGame->update(SGIDX_BONUSGAME,BGBOTH);

		initializeMonsterBuilder();
		readytofinish = false;
	}
}


void
MonsterSlotGuts::initializeGraveBonus()
{
		m_subGame->update(SGIDX_PAYOUT1,0);
		m_subGame->update(SGIDX_PAYOUT2,0);
		m_subGame->update(SGIDX_PAYOUT3,0);
		m_subGame->update(SGIDX_SELECTION,0);
		m_subGame->update(SGIDX_PAYOUT,0);
}


void
MonsterSlotGuts::initializeMonsterBuilder()
{
	m_subGame->update(SGIDX_TRAY1,0);
	m_subGame->update(SGIDX_TRAY2,0);
	m_subGame->update(SGIDX_TRAY3,0);
	m_subGame->update(SGIDX_TRAY4,0);
	m_subGame->update(SGIDX_TRAY5,0);
	m_subGame->update(SGIDX_TRAY6,0);
	m_subGame->update(SGIDX_TRAYBONUSAMOUNT,0);
	m_subGame->update(SGIDX_TRAYSELECTIONDONE,0);
	m_subGame->update(SGIDX_TUBE1MONSTER,0);
	m_subGame->update(SGIDX_TUBE1BONUS,0);
	m_subGame->update(SGIDX_TUBE2MONSTER,0);
	m_subGame->update(SGIDX_TUBE2BONUS,0);
	m_subGame->update(SGIDX_TUBE3MONSTER,0);
	m_subGame->update(SGIDX_TUBE3BONUS,0);
	m_subGame->update(SGIDX_ROUNDNUMBER,0);
	m_subGame->update(SGIDX_PLAYERSELECTION,0);
	m_subGame->update(SGIDX_PLAYERPAYOUT,0);
	m_subGame->update(SGIDX_TOTALPAYOUT,0);
	m_subGame->update(SGIDX_SUBGAMEOVER,0);
}


bool8
MonsterSlotGuts::playGraveBonus(uint8 bonusGameType, int32 bonusDataCount, int32 *bonusData)
{
	int32 bonusGame;
	bool8 problem = false;

	currentGameNum = (getSlotSpin(getSlotSpinCount()-1))->getGameNum();
	bonusGame = m_subGame->getData(SGIDX_BONUSGAME);
	if(((bonusGame == MonsterSlotGuts::BGGRAVEBONUS) || (bonusGame == MonsterSlotGuts::BGBOTH)) &&
	(m_subGame->getData(SGIDX_SELECTION) == 0) && (bonusDataCount == 1) &&
	((bonusGameType == MonsterSlotGuts::BGGRAVEBONUS) || (bonusGameType == MonsterSlotGuts::BGBOTH))
	&& ((*bonusData > 0) && (*bonusData < 4)))
	{
		int32 payout;

		// For now we will just get 3 random values as the payouts
		payout = getrand() % 200;

		if (*bonusData == 1)
		{
			m_subGame->update(SGIDX_PAYOUT1,payout);
		}
		else
		{
			m_subGame->update(SGIDX_PAYOUT1,getrand() % 200);
		}
		if (*bonusData == 2)
		{
			m_subGame->update(SGIDX_PAYOUT2,payout);
		}
		else
		{
			m_subGame->update(SGIDX_PAYOUT2,getrand() % 200);
		}
		if (*bonusData == 3)
		{
			m_subGame->update(SGIDX_PAYOUT3,payout);
		}
		else
		{
			m_subGame->update(SGIDX_PAYOUT3,getrand() % 200);
		}

		m_subGame->update(SGIDX_SELECTION,*bonusData);
		m_subGame->update(SGIDX_PAYOUT,payout);

		createBonusGame(currentGameNum,10,1,BGGRAVEBONUS,*bonusData,payout);

		if (bonusGame != MonsterSlotGuts::BGBOTH)
		{
			readytofinish = true;
			if(isGameComplete())
			{
				completeGame();
	/*
				if (bonusGame == MonsterSlotGuts::BGBOTH)
				{
					m_subGame->update(SGIDX_BONUSGAME,BGBUILDERBONUS);
				}
				else
				{
					m_subGame->update(SGIDX_BONUSGAME,BGNONE );
				}
	*/
			}
		}
		else
		{
			m_subGame->update(SGIDX_BONUSGAME,BGBUILDERBONUS);
		}
	}
	else
	{
		problem = true;
		Log::msgWarning("Grave Bonus - Trying to play with bad bonus game type: %d, len: %d, data: %d", currentGameNum, bonusDataCount, *bonusData);
	}
	return (problem);
}

bool8
MonsterSlotGuts::playMonsterBuilder(uint8 bonusGameType, int32 bonusDataCount, int32 *bonusData)
{
	int32 bonusGame;
	int32 round;
	bool8 problem = false;

	currentGameNum = (getSlotSpin(getSlotSpinCount()-1))->getGameNum();
	bonusGame = m_subGame->getData(SGIDX_BONUSGAME);
	round = m_subGame->getData(SGIDX_ROUNDNUMBER);

	if(((bonusGame == MonsterSlotGuts::BGBUILDERBONUS) || (bonusGame == MonsterSlotGuts::BGBOTH)) &&
	(m_subGame->getData(SGIDX_ROUNDNUMBER) < 14) && (bonusDataCount == 1) &&
	((bonusGameType == MonsterSlotGuts::BGBUILDERBONUS) || (bonusGameType == MonsterSlotGuts::BGBOTH))
	&& ((*bonusData >= 0) && (*bonusData < 6)))
	{
		if (m_subGame->getData(SGIDX_TRAYSELECTIONDONE) == 0)
		{
			int32 tray;
			int32 traycount = 0;
			int32 value = getrand() % 200;

			tray = *bonusData;

			if (tray>=0 && tray < 6)
			{
				traycount = m_subGame->getData(SGIDX_TRAY1) + 
							m_subGame->getData(SGIDX_TRAY2) + 
							m_subGame->getData(SGIDX_TRAY3) + 
							m_subGame->getData(SGIDX_TRAY4) + 
							m_subGame->getData(SGIDX_TRAY5) + 
							m_subGame->getData(SGIDX_TRAY6);

				m_subGame->update(SGIDX_TRAY1+tray,1);
				m_subGame->update(SGIDX_TRAYBONUSAMOUNT,m_subGame->getData(SGIDX_TRAYBONUSAMOUNT)+value);
				Log::msgInformation("Monster Builder select trays: %d,tray: %d value: %d", traycount, tray, value);

				if (traycount == 2)
				{
					m_subGame->update(SGIDX_TRAYSELECTIONDONE,1);
				}
				createBonusGame(currentGameNum,10,round*10+tray+1,BGBUILDERBONUS,tray,value);

			}
			else
			{
				Log::msgInformation("Monster Builder select trays invalid player response: %d", tray);
			}
		}
		else
		{
			int32 monster[3];
			int32 bonus[3];
			int32 tube;

			tube = *bonusData;
//			round = m_subGame->getData(SGIDX_ROUNDNUMBER) + 1;
			++round;
			m_subGame->update(SGIDX_ROUNDNUMBER, round);

			monster[0] = monster[1] = monster[2] = gMonster++%14;
/*
			monster[0] = getrand() % 13;
			while ((monster[1] = getrand() % 13) == monster[0])
				;
			while (((monster[2] = getrand() % 13) == monster[0]) || 
				(monster[2] == monster[1]))
				;
*/
			Log::msgInformation("Monster Builder player selected tube: %d", tube);
			Log::msgInformation("Monster Builder tube0 monster: %d, tube1 monster: %d, tube2 monster: %d", monster[0], monster[1], monster[2]);

			bonus[0] = m_subGame->getData(SGIDX_TRAYBONUSAMOUNT) * monster[0];
			bonus[1] = m_subGame->getData(SGIDX_TRAYBONUSAMOUNT) * monster[1];
			bonus[2] = m_subGame->getData(SGIDX_TRAYBONUSAMOUNT) * monster[2];

			Log::msgInformation("Monster Builder bonus0: %d, bonus1: %d, bonus2: %d", bonus[0], bonus[1], bonus[2]);

			m_subGame->update(SGIDX_TUBE1MONSTER, monster[0]);
			m_subGame->update(SGIDX_TUBE1BONUS, bonus[0]);

			m_subGame->update(SGIDX_TUBE2MONSTER, monster[1]);
			m_subGame->update(SGIDX_TUBE2BONUS, bonus[1]);

			m_subGame->update(SGIDX_TUBE3MONSTER, monster[2]);
			m_subGame->update(SGIDX_TUBE3BONUS, bonus[2]);

			m_subGame->update(SGIDX_PLAYERSELECTION, tube);
			m_subGame->update(SGIDX_PLAYERPAYOUT, bonus[tube]);
			
			m_subGame->update(SGIDX_TOTALPAYOUT, m_subGame->getData(SGIDX_TOTALPAYOUT)+m_subGame->getData(SGIDX_PLAYERPAYOUT));

			m_subGame->update(SGIDX_TRAY1, 0);
			m_subGame->update(SGIDX_TRAY2, 0);
			m_subGame->update(SGIDX_TRAY3, 0);
			m_subGame->update(SGIDX_TRAY4, 0);
			m_subGame->update(SGIDX_TRAY5, 0);
			m_subGame->update(SGIDX_TRAY6, 0);
			m_subGame->update(SGIDX_TRAYBONUSAMOUNT, 0);
			m_subGame->update(SGIDX_TRAYSELECTIONDONE, 0);

			createBonusGame(currentGameNum,10,round*10+7+tube,BGBUILDERBONUS,tube,bonus[tube]);

			if (round == 4)
			{
//				m_subGame->update(SGIDX_BONUSGAME, BGNONE);

				m_subGame->update(SGIDX_FEET, FEET_START);
				m_subGame->update(SGIDX_HAND, HAND_START);
				m_subGame->update(SGIDX_SKULL, SKULL_START);
				m_subGame->update(SGIDX_LIVER, LIVER_START);
				m_subGame->update(SGIDX_INTESTINES, INTESTINES_START);
				m_subGame->update(SGIDX_EYEBALLS, EYEBALLS_START);
				m_subGame->update(SGIDX_LUNGS, LUNGS_START);
				m_subGame->update(SGIDX_HEART, HEART_START);
				m_subGame->update(SGIDX_BRAIN, BRAIN_START);

				m_subGame->update(SGIDX_SUBGAMEOVER,1);
				readytofinish = true;

				if(isGameComplete())
				{
					completeGame();
				}
			}
		}
	}
	else
	{
		problem = true;
		Log::msgWarning("Monster Builder - Trying to play with bad bonus game: %d, len: %d, data: %d", 
			currentGameNum, bonusDataCount, *bonusData);
	}

	return (problem);
}

void
MonsterSlotGuts::playBonusGame(uint8 bonusGameType, int32 bonusDataCount, int32 *bonusData)
{
	int32 bonusGame;
	bool8 problem = false;

	bonusGame = m_subGame->getData(SGIDX_BONUSGAME);

	if ((bonusGame == BGGRAVEBONUS || bonusGame == BGBOTH) && bonusGameType == BGGRAVEBONUS)
	{
		playGraveBonus(bonusGameType, bonusDataCount, bonusData);
	}
	else if (bonusGame == BGBUILDERBONUS && bonusGameType == BGBUILDERBONUS)
	{
		playMonsterBuilder(bonusGameType, bonusDataCount, bonusData);
	}
//	else if (bonusGame == BGBOTH)
//	{
//		playMonsterBuilder(bonusGameType, bonusDataCount, bonusData);
//	}
	else
	{
		problem = true;
		Log::msgWarning("Monster - Trying to play bonus game when not in a bonus game: %d, bonusGame: %d, bonusGameType: %d", 
			currentGameNum, bonusGame, bonusGameType);
	}
}


void
MonsterSlotGuts::loadSubGameData()
{
	int32 count;

	SlotGuts::loadSubGameData();

	count = m_subGame->getDataCount();

	if (count == 0)
	{
		m_subGame->update(SGIDX_BONUSGAME, BGNONE);

		m_subGame->update(SGIDX_FEET, FEET_START);
		m_subGame->update(SGIDX_HAND, HAND_START);
		m_subGame->update(SGIDX_SKULL, SKULL_START);
		m_subGame->update(SGIDX_LIVER, LIVER_START);
		m_subGame->update(SGIDX_INTESTINES, INTESTINES_START);
		m_subGame->update(SGIDX_EYEBALLS, EYEBALLS_START);
		m_subGame->update(SGIDX_LUNGS, LUNGS_START);
		m_subGame->update(SGIDX_HEART, HEART_START);
		m_subGame->update(SGIDX_BRAIN, BRAIN_START);

		m_subGame->update(SGIDX_REEL1, 0);
		m_subGame->update(SGIDX_LINE1, 0);
		m_subGame->update(SGIDX_REEL2, 0);
		m_subGame->update(SGIDX_LINE2, 0);
		m_subGame->update(SGIDX_REEL3, 0);
		m_subGame->update(SGIDX_LINE3, 0);

		initializeGraveBonus();

		initializeMonsterBuilder();
	}
	else
	{
		if ((m_subGame->getData (SGIDX_BONUSGAME) == BGGRAVEBONUS) && (m_subGame->getData (SGIDX_SELECTION) == 1))
		{
			m_subGame->update(SGIDX_BONUSGAME, BGNONE);

			m_subGame->update(SGIDX_REEL1, 0);
			m_subGame->update(SGIDX_LINE1, 0);
			m_subGame->update(SGIDX_REEL2, 0);
			m_subGame->update(SGIDX_LINE2, 0);
			m_subGame->update(SGIDX_REEL3, 0);
			m_subGame->update(SGIDX_LINE3, 0);
			initializeGraveBonus();
		}
		else if ((m_subGame->getData (SGIDX_BONUSGAME) == BGBOTH) && (m_subGame->getData (SGIDX_SELECTION) == 1))
		{
			m_subGame->update(SGIDX_BONUSGAME, BGBUILDERBONUS);
			initializeMonsterBuilder();
		}
		else if ((m_subGame->getData (SGIDX_BONUSGAME) == BGBUILDERBONUS) && (m_subGame->getData (SGIDX_SUBGAMEOVER) == 1))
		{
			m_subGame->update(SGIDX_BONUSGAME, BGNONE);
			initializeMonsterBuilder();
		}
	}
}


bool8
MonsterSlotGuts::saveGame()
{
	//Base class
	bool8 result = SlotGuts::saveGame();

	if(result && !readytofinish)
	{
		//get the gamenum
		currentGameNum = (getSlotSpin(getSlotSpinCount()-1))->getGameNum();

		Log::msgInformation("Monster Builder saveGame: %d", currentGameNum);
 
		if(m_subGame->getData(SGIDX_BONUSGAME) == BGGRAVEBONUS)
		{
			if (!createBonusGame(currentGameNum,10,0,BGGRAVEBONUS,m_subGame->getData(SGIDX_SELECTION),m_subGame->getData(SGIDX_PAYOUT)))
			{
				result = false;
			}
		}
		else if(m_subGame->getData(SGIDX_BONUSGAME) == BGBUILDERBONUS)
		{
			if (!createBonusGame(currentGameNum,10,0,BGBUILDERBONUS,0,0))
			{
				result = false;
			}
		}
		else
		{
		}
	}
	return result;
}

bool8
MonsterSlotGuts::isGameComplete()
{
    return readytofinish;
}

